package com.xbk.seckill.order.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.xbk.seckill.common.constants.SeckillConstants;
import com.xbk.seckill.common.exception.ErrorCode;
import com.xbk.seckill.common.exception.SeckillException;
import com.xbk.seckill.common.model.enums.SeckillOrderStatus;
import com.xbk.seckill.mq.MessageSenderService;
import com.xbk.seckill.order.domain.event.SeckillOrderEvent;
import com.xbk.seckill.order.domain.model.entity.SeckillOrder;
import com.xbk.seckill.order.domain.repository.SeckillOrderRepository;
import com.xbk.seckill.order.domain.service.SeckillOrderDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author xiexu
 * @date 2025/2/5 17:16
 */
@Service
@Slf4j
public class SeckillOrderDomainServiceImpl implements SeckillOrderDomainService {

    @Autowired
    private SeckillOrderRepository seckillOrderRepository;

    @Autowired
    private MessageSenderService messageSenderService;

    @Value("${message.mq.type}")
    private String eventType;

    /**
     * 保存订单到数据库
     *
     * @param seckillOrder
     * @return
     */
    @Override
    public boolean saveSeckillOrder(SeckillOrder seckillOrder) {
        if (Objects.isNull(seckillOrder)) {
            throw new SeckillException(ErrorCode.PARAMS_INVALID);
        }
        log.info("saveSeckillOrder|下单|{}", JSON.toJSONString(seckillOrder));
        // 设置订单状态为已创建
        seckillOrder.setStatus(SeckillOrderStatus.CREATED.getCode());
        // 保存订单到数据库
        boolean saveSuccess = seckillOrderRepository.saveSeckillOrder(seckillOrder);
        if (saveSuccess) {
            log.info("saveSeckillOrder|创建订单成功|{}", JSON.toJSONString(seckillOrder));
            // 组装保存订单事件
            SeckillOrderEvent seckillOrderEvent = new SeckillOrderEvent(seckillOrder.getId(),
                    SeckillOrderStatus.CREATED.getCode(), getTopicEvent());
            // 发布订单事件
            messageSenderService.send(seckillOrderEvent);
        }
        return saveSuccess;
    }

    /**
     * 根据用户id获取订单列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<SeckillOrder> getSeckillOrderByUserId(Long userId) {
        if (Objects.isNull(userId)) {
            throw new SeckillException(ErrorCode.PARAMS_INVALID);
        }
        // 获取订单列表
        return seckillOrderRepository.getSeckillOrderByUserId(userId);
    }

    /**
     * 根据商品id获取订单列表
     *
     * @param goodsId
     * @return
     */
    @Override
    public List<SeckillOrder> getSeckillOrderByGoodsId(Long goodsId) {
        if (Objects.isNull(goodsId)) {
            throw new SeckillException(ErrorCode.PARAMS_INVALID);
        }
        // 获取订单列表
        return seckillOrderRepository.getSeckillOrderByGoodsId(goodsId);
    }

    /**
     * 以userId作为分片键删除订单
     *
     * @param orderId
     * @param userId
     */
    @Override
    public void deleteOrderShardingUserId(Long orderId, Long userId) {
        if (Objects.isNull(orderId) || Objects.isNull(userId)) {
            throw new SeckillException(ErrorCode.PARAMS_INVALID);
        }
        seckillOrderRepository.deleteOrderShardingUserId(orderId, userId);
    }

    /**
     * 以goodsId作为分片键删除订单
     *
     * @param orderId
     * @param goodsId
     */
    @Override
    public void deleteOrderShardingGoodsId(Long orderId, Long goodsId) {
        if (Objects.isNull(orderId) || Objects.isNull(goodsId)) {
            throw new SeckillException(ErrorCode.PARAMS_INVALID);
        }
        // 删除订单
        seckillOrderRepository.deleteOrderShardingGoodsId(orderId, goodsId);
    }

    /**
     * 获取主题事件
     */
    private String getTopicEvent() {
        return SeckillConstants.EVENT_PUBLISH_TYPE_ROCKETMQ.equals(eventType)
                ? SeckillConstants.TOPIC_EVENT_ROCKETMQ_ORDER
                : SeckillConstants.TOPIC_EVENT_COLA;
    }
}
